package com.mspbots.script.event;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mspbots.api.client.CoreClient;
import com.mspbots.script.model.SystemScript;
import com.mspbots.script.model.TenantScript;
import com.mspbots.script.model.TriggerEventDTO;
import com.mspbots.script.service.ScriptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.script.ScriptException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
@Component
public class TriggerEventConsumer {

    @Autowired
    private CoreClient coreClient;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private ScriptService scriptService;


    @RabbitListener(queues = "trigger.event", containerFactory = "customContainerFactory")
    public void onRule(TriggerEventDTO triggerEventDTO) {
        try {
            if ("System".equalsIgnoreCase(triggerEventDTO.getScope())) {
                Map<String, Object> params = Map.of(
                        "triggerEvent", triggerEventDTO.getEventName(),
                        "scope", triggerEventDTO.getScope(),
                        "enable", true);
                JsonNode jsonNode = this.coreClient.get("/system-script/list", params).block();
                List<SystemScript> list = objectMapper.convertValue(jsonNode, new TypeReference<List<SystemScript>>() {
                });
                if (!CollectionUtils.isEmpty(list)) {
                    List<TenantScript> tenantScripts = new ArrayList<>();
                    for (SystemScript systemScript : list) {
                        TenantScript tenantScript = new TenantScript();
                        BeanUtils.copyProperties(systemScript, tenantScript);
                        tenantScript.setTenantId(triggerEventDTO.getTenantId());
                        tenantScript.setSystemScriptId(systemScript.getId());
                        tenantScripts.add(tenantScript);
                    }
                    log.debug("exe system trigger");
                    scriptService.execute(tenantScripts, triggerEventDTO);
                }
            } else {
                if(ObjectUtils.isEmpty(triggerEventDTO.getEventName())){
                    log.info("triggerEventDTO getEventName is null:");
                    return;
                }

                log.debug("params:{}",triggerEventDTO);
                Map<String, Object> params = Map.of(
                        "tenantId", triggerEventDTO.getTenantId(),
                        "triggerEvent", triggerEventDTO.getEventName(),
                        "scope", triggerEventDTO.getScope(),
                        "enable", true);


                JsonNode jsonNode = this.coreClient.get("/script/list", params).block();


                List<TenantScript> list = objectMapper.convertValue(jsonNode, new TypeReference<List<TenantScript>>() {
                });
                if (!CollectionUtils.isEmpty(list)) {
                    scriptService.execute(list, triggerEventDTO);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
